﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameViewer.Core.Archives;

namespace GameViewer.Ogre
{
    public class OgreMeshSerializer : OgreSerializer
    {
        Dictionary<string, OgreMeshSerializerImpl> mImplementations;
        protected static string msCurrentVersion = "[MeshSerializer_v1.41]"; 
        const ushort HEADER_CHUNK_ID = 0x1000;

        public OgreMeshSerializer()
        {
            mImplementations = new Dictionary<string, OgreMeshSerializerImpl>();
            mImplementations.Add("[MeshSerializer_v1.10]", new OgreMeshSerializerImpl_V1_1());
            mImplementations.Add("[MeshSerializer_v1.20]", new OgreMeshSerializerImpl_V1_2());
            mImplementations.Add("[MeshSerializer_v1.30]", new OgreMeshSerializerImpl_V1_3());
            mImplementations.Add("[MeshSerializer_v1.40]", new OgreMeshSerializerImpl_V1_4());
            mImplementations.Add(msCurrentVersion, new OgreMeshSerializerImpl());
        }

        public void ImportMesh(System.IO.BinaryReader reader, OgreMesh mesh)
        {
            DetermineEndianness(reader);

            // Read header and determine the version
            ushort headerID;

            // Read header ID
            ReadShorts(reader, out headerID, 1);

            if (headerID != HEADER_CHUNK_ID)
            {
                //OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR, "File header not found",
                //    "MeshSerializer::importMesh");
            }
            // Read version
            String ver = ReadString(reader);
            // Jump back to start
            reader.BaseStream.Position = 0;

             // Find the implementation to use
            if(mImplementations.ContainsKey(ver))
            {
                mImplementations[ver].ImportMesh(reader, mesh);
            }
            else
            {
                //
            }
        }
    }
}
